// RSA	加密文件
import JSEncrypt from './jsencrypt.min.js';
export default {
	// 获取cookie指定字符
	getCookieValue(cookieName) {
	  var cookies = document.cookie.split(';');
	  for (var i = 0; i < cookies.length; i++) {
	    var cookie = cookies[i].trim();
	    if (cookie.indexOf(cookieName) === 0) {
	      return cookie.substring(cookieName.length + 1, cookie.length);
	    }
	  }
	  return null;
	},
	// 获取地址栏指定字符参数
	getQueryVariable(variable,query) {
		var rooms = query.split('?');
		for (var i = 0; i < rooms.length; i++) {
			var pair = rooms[i].split('=');
			if (pair[0] == variable) {
				return (pair[1].split('&'))[0]
			}
		}
		//或者拼接&后面的值
		var vars = query.split('&')
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split('=')
			if (pair[0] == variable) {
				return pair[1]
			}
		}
	},
	// 计算某个值的总和
	countTotal(arr, keyName) {
	  let $total = 1
	  $total = arr.reduce(function(total, currentValue, currentIndex, arr) {
	    return currentValue[keyName] ? (total + currentValue[keyName]) : total
	  }, 0)
	  return $total
	},
	// 手机号加密显示
	mobil_encry(mobile){
		if(!mobile){
			return
		}
		let encry_mobile = mobile.substr(0,3) + "****" + mobile.substr(7)
		return encry_mobile
	},
	/*加密方法  data需要加密的文本 ，key后端返回的公钥 */
	getEncrypt(data,key) {
	   let encrypt = new JSEncrypt()
	   // 设置公钥
	   encrypt.setPublicKey(key)
	   return encrypt.encrypt(data)
	},
	// 解密
	decrypt(txt) {
	  let privateKey	= 'MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJkQK2U1BgHJopEJDtiAp215HDL+t1ixxc4F2b+3Ex6AyehzBjGdSIsaWIxGLHYz89ahehbRKAMzwekGwn6C2Htuv5WNxLCgXtlXVGErLInUcFgs5EaQDlEfYOuSzVUisZ6mcnVxGXcgse7huuRqzzrZgoOfIBWoFuoqNgGFsRYrAgMBAAECgYBreBXwDqW0FZ4GFYUN8G8eBz4MdBnJtbA78pGr8WAEN8sMYc7EgTmRKaNoglDvgZTAa0pswNhGP030TqG68CYMlJXrA3psVKVOKf9U+4P9X38KeGE+aDsyJ4He20B93fiihdcQRFlrGMN6Ur6MxdzHrC3Po0KxhSIJ2lRYU6RE4QJBAN6gWTGgLZ3jSoOxoi5YTYVuD5S4jQBWZmh0Q83EssWsK5VPmuyFLbcWAq8w9zW1CwXA24/8vPlwWGY59v2sWDsCQQCwAjgkAtIjOa1Z9hpF1QmOer+FXM5uwemCSffhyf1ZqexV2h9j+VDRAHEVaNUktwnXKgV5TXqypcvRUDejtErRAkA/sWSTXB5w85xX/TRHSiAzsn9mrz44AIAgFlNnxYTYJR/7AeFzFc80eGmgMcQoa3mR1dF7anWub8P6y06s2OgNAkAkFkBmecGWAVRelovqd3R/JwGvmUimAtIaqYnOXHi6cpGHRLKc79g3Ee0do6HX5kN5Px9iMJWQnVe9scHGqceRAkBPrlI7oAP2WvxFLkP1LExmroB1gm4/PpmncUf4oA5qr5mOW8GpyNW990RpvWmzE8HoYoEEHDbxI5gpPEDsTUQY'
	  const encryptor = new JSEncrypt()
	  encryptor.setPrivateKey(privateKey) // 设置私钥
	  return encryptor.decrypt(txt) // 对数据进行解密
	},
	// 数组去重 数组:arr , uniId 去重字段
	uniqueFunc(arr, uniId){
	  const res = new Map();
	  return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
	},
	// 复制文本
	copy_text(e){
		uni.setClipboardData({
			data: e,
			success: () => {
				uni.showToast({
					icon: 'success',
					title: '复制成功'
				});
			}
		});
	},
	// 打电话
	callPhone(phoneNumber = '') {
		let num = phoneNumber.toString()
		uni.makePhoneCall({
			phoneNumber: num,
			fail(err) {
				console.log('makePhoneCall出错', err)
			},
		});
	},
	parseTime(time, pattern) {
		if (time === '0001-01-01T00:00:00Z') return ''
		if (arguments.length === 0 || !time) {
			return null
		}
		const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
		let date
		if (typeof time === 'object') {
			date = time
		} else {
			if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
				time = parseInt(time)
			}
			if ((typeof time === 'number') && (time.toString().length === 10)) {
				time = time * 1000
			}
			date = new Date(time)
		}
		const formatObj = {
			y: date.getFullYear(),
			m: date.getMonth() + 1,
			d: date.getDate(),
			h: date.getHours(),
			i: date.getMinutes(),
			s: date.getSeconds(),
			a: date.getDay()
		}
		const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
			let value = formatObj[key]
			// Note: getDay() returns 0 on Sunday
			if (key === 'a') {
				return ['日', '一', '二', '三', '四', '五', '六'][value]
			}
			if (result.length > 0 && value < 10) {
				value = '0' + value
			}
			return value || 0
		})
		return time_str
	},
	/**提示框
	 *title(标题)
	 *icon(图标):  success，loading，none
	 *duration(延时): 0为不关闭, 毫秒数
	 *options(其它参数)
	 */
    msg(title, duration=2000, mask=false, icon='none'){
    	//统一提示方便全局修改
    	if(Boolean(title) === false){
    		return;
    	}
    	uni.showToast({
    		title,
    		duration,
    		mask,
    		icon
    	});
    },
/**
 * 获取页面组件对应的信息
 * params:
 * params.name 节点的标识
 * params.success 成功后回调函数
 */
 getComponentInfo(params) {
  uni.createSelectorQuery().in(params.component).select(params.name).boundingClientRect((rect) => {
    params.success && params.success(rect);
  }).exec();
},
	windowPostMsg(title, url) {
		console.log(title)
		console.log(url)
		window.top.postMessage({
			title: title,
			url: window.location.origin + url,
			open: true
		}, '*')
	}

}
